package game;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;

public class NewLevel extends Scene
{

	//SoundClip som;
	Point sizeChip = new Point(300, 300);
	Point sizeCapacitor = new Point(300, 112);
	Point sizeOpcao = new Point(200, 75);
	Point2D posCapacitorEsq = new Point2D.Double(-this.sizeCapacitor.getX(), (Game.screenHeigth/2) - (this.sizeCapacitor.getY()/2));
	Point2D posCapacitorDir = new Point2D.Double(Game.screenWidth, (Game.screenHeigth/2) - (this.sizeCapacitor.getY()/2));
	Double velocity = 500.0;
	int limiteCapacitorEsq;
	int limiteCapacitorDir;
	boolean animacaoCapacitorCompleta = false;
	boolean animacaoTextoCompleta = false;
	
	float timer = 0;
	
	Font font1 = LoadFonte.getFonte(2);
	FontMetrics fm;
	int fontWidth;
	String texto;
	Point2D posTexto = new Point2D.Double((Game.screenWidth/2) - (fontWidth/2), -font1.getSize() - 10);
	int limiteTexto = 30 + font1.getSize();
	
	public NewLevel()
	{
		this.limiteCapacitorDir = Game.screenWidth - 355;
		this.limiteCapacitorEsq = 355 - (int)this.sizeCapacitor.getX();
		texto = "Novo Level";
	}
	
	@Override
	public void update() 
	{
		if(this.posCapacitorDir.getX() > this.limiteCapacitorDir)
			this.posCapacitorDir = new Point2D.Double(this.posCapacitorDir.getX() - (this.velocity*Time.getInstance().getLoopTime()), this.posCapacitorDir.getY());
		else
			this.posCapacitorDir = new Point2D.Double(this.limiteCapacitorDir, this.posCapacitorDir.getY());
		
		if(this.posCapacitorDir.getX() < this.limiteCapacitorDir)
			this.posCapacitorDir = new Point2D.Double(this.limiteCapacitorDir, this.posCapacitorDir.getY());
		
		
		if(this.posCapacitorEsq.getX() < this.limiteCapacitorEsq)
			this.posCapacitorEsq = new Point2D.Double(this.posCapacitorEsq.getX() + (this.velocity*Time.getInstance().getLoopTime()), this.posCapacitorEsq.getY());
		else
		{
			this.posCapacitorEsq = new Point2D.Double(this.limiteCapacitorEsq, this.posCapacitorEsq.getY());
			animacaoCapacitorCompleta = true;
		}
		if(this.posCapacitorEsq.getX() > this.limiteCapacitorEsq)
			this.posCapacitorEsq = new Point2D.Double(this.limiteCapacitorEsq, this.posCapacitorEsq.getY());
		
		
		if(this.animacaoCapacitorCompleta && !this.animacaoTextoCompleta)
		{
			if(this.posTexto.getY() < this.limiteTexto)
				this.posTexto = new Point2D.Double(this.posTexto.getX(),
								this.posTexto.getY() + (this.velocity*Time.getInstance().getLoopTime()));
			else
			{
				this.posTexto = new Point2D.Double(this.posTexto.getX(),
						this.limiteTexto);
				timer += Time.getInstance().getLoopTime();
				
				
				if(timer >= 1.5 && timer < 2)
				{
					Audios.getInstance().Play(4);
					//som = new SoundClip(4, 0);
					timer = 2.1f;
				}
				if(timer >= 3)
				{
					this.animacaoTextoCompleta = true;
					timer = 0;
				}
			}
			if(this.posTexto.getY() > this.limiteTexto)
				this.posTexto = new Point2D.Double(this.posTexto.getX(),
						this.limiteTexto);
		}
		
		if(animacaoTextoCompleta)
		{
			SceneManager.newLevel();
		}
		
	}

	@Override
	public void draw(Graphics2D g2d) 
	{
		//Background
		g2d.drawImage(LoadImages.getImage(41), 0, 0, null);
		
		//capacitor direita
		g2d.drawImage(LoadImages.getImage(43),
				(int)this.posCapacitorDir.getX(),
				(int)this.posCapacitorDir.getY(), null);
		
		//capacitor esquerda
				g2d.drawImage(LoadImages.getImage(44),
						(int)this.posCapacitorEsq.getX(),
						(int)this.posCapacitorEsq.getY(), null);
		
		//Chip Center
		g2d.drawImage(LoadImages.getImage(42),
				(Game.screenWidth/2) - (int)(this.sizeChip.getX()/2),
				(Game.screenHeigth/2) - (int)(this.sizeChip.getY()/2), null);
		
		
		
		if(this.animacaoCapacitorCompleta)
		{
			g2d.setFont(font1);
			g2d.setColor(Color.RED);
			fm = g2d.getFontMetrics();
			fontWidth = fm.stringWidth(this.texto);
			this.posTexto = new Point2D.Double((Game.screenWidth/2) - (fontWidth/2), this.posTexto.getY());
			g2d.drawString(this.texto, (int)this.posTexto.getX(), (int)this.posTexto.getY());
		}
	}
}
